package com.ruoyi.wechat.handler;

import com.ruoyi.wechat.domain.WxUser;
import com.ruoyi.wechat.service.MsgReplyService;
import com.ruoyi.wechat.service.WxUserService;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @author Binary Wang
 */
@Component
public class SubscribeHandler extends AbstractHandler {

    @Autowired
    private WxUserService wxUserService;
    @Resource
    MsgReplyService msgReplyService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {

        this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser() + "，事件：" + wxMessage.getEventKey());
        String appid = WxMpConfigStorageHolder.get();
        this.logger.info("appid:{}", appid);
        wxUserService.refreshUserInfo(wxMessage.getFromUser(), appid);
//
        msgReplyService.tryAutoReply(appid, true, wxMessage.getFromUser(), wxMessage.getEvent());

        if (StringUtils.hasText(wxMessage.getEventKey())) {
            logger.info("扫描代参二维码关注:{}", wxMessage.getEventKey());
            // 处理特殊事件，如用户扫描带参二维码关注
            if (wxMessage.getEventKey().contains("qrscene_")) {
                logger.info("带参扫码进来的:{}", wxMessage.getEventKey());
                wxMessage.setEventKey(wxMessage.getEventKey().replace("qrscene_", ""));
            }
            msgReplyService.tryAutoReply(appid, true, wxMessage.getFromUser(), wxMessage.getEventKey());
        }
        return null;
    }

    /**
     * 处理特殊请求，比如如果是扫码进来的，可以做相应处理
     */
    protected WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage) {
        this.logger.info("特殊请求-新关注用户 OPENID: " + wxMessage.getFromUser());
        //对关注事件和扫码事件分别处理
        String appid = WxMpConfigStorageHolder.get();
        wxUserService.refreshUserInfo(wxMessage.getFromUser(), appid);
        msgReplyService.tryAutoReply(appid, true, wxMessage.getFromUser(), wxMessage.getEvent());
        if (StringUtils.hasText(wxMessage.getEventKey())) {
            msgReplyService.tryAutoReply(appid, true, wxMessage.getFromUser(), wxMessage.getEventKey());
        }
        return null;
    }

    public static void setWxUserValue(WxUser wxUser, WxMpUser userWxInfo) {
        wxUser.setSubscribe("1");
        wxUser.setSubscribeScene(userWxInfo.getSubscribeScene());
        wxUser.setSubscribeTime(new Date(userWxInfo.getSubscribeTime() * 1000));
        wxUser.setOpenid(userWxInfo.getOpenId());
        wxUser.setSex("2");
        wxUser.setRemark(userWxInfo.getRemark());
        wxUser.setUnionid(userWxInfo.getUnionId());
        Long[] tagIds = userWxInfo.getTagIds();
        wxUser.setTagidList(com.ruoyi.common.utils.StringUtils.join(tagIds,","));
        wxUser.setQrSceneStr(userWxInfo.getQrSceneStr());
    }

}
